import java.util.Arrays;

public class MyArrayList implements IList {
    public int[]  elem;
    public int usesize;
    public static final int DEFAULT_CAPACITY = 5;

    public MyArrayList() {
        this.elem = new int[DEFAULT_CAPACITY];
    }
public boolean isFull(){
        return elem.length == usesize;
}
    @Override
    public void add(int data) {
        if (isFull()){
            elem =Arrays.copyOf(elem,2*elem.length);
        }
        elem[usesize] = data;
        usesize++;
    }
private void checkPos(int pos){
        if (pos < 0 || pos > usesize){
            throw new PosException("pos位置不合法"+pos);
        }

}
    @Override
    public void add(int pos, int data) {
       checkPos(pos);
        if (isFull()){
            elem =Arrays.copyOf(elem,2*elem.length);
        }
        for (int i = usesize - 1; i >= pos ; i--) {
            elem[i+1] = elem[i];
        }
        elem[pos] = data;
        usesize++;
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i <usesize ; i++) {
            if (elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i <usesize ; i++) {
            if (elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }
public boolean isEmpty(){
        return usesize == 0;
}
    @Override
    public int get(int pos) {
        checkPos(pos);
        if (isEmpty()){
            throw new EmptyException("顺序表为空");
        }
        for (int i = 0; i <usesize ; i++) {
            return elem[pos];
        }
        return -1;
    }

    @Override
    public void set(int pos, int value) {
        checkPos(pos);
        if (isEmpty()){
            throw new EmptyException("顺序表为空");
        }
        elem[pos] = value;

    }

    @Override
    public void remove(int toRemove) {
        if (isEmpty()){
            throw new EmptyException("顺序表为空");
        }
        int index = indexOf(toRemove);
        for (int i = index; i <usesize - 1 ; i++) {
            elem[i] = elem[i+1];
        }
        usesize--;
    }

    @Override
    public int size() {
        return this.usesize;
    }

    @Override
    public void clear() {
      usesize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i <usesize ; i++) {
            System.out.println(elem[i]);
        }

        }
}
